/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Examples;

import TextBeans.DocTitle;
import java.awt.Color;
import java.awt.Font;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author Cesar
 */
public class JTextEditorV1 extends java.awt.Frame {

    /**
     * Creates new form JTextEditorV1
     */
 
    public Font ObtainFont(int FontNum, int Size){
        Font CurrentFont;
        switch(FontNum){
            case 0: CurrentFont = new Font("Bold", Font.BOLD, Size);
                break;
            case 1: CurrentFont = new Font("Italic", Font.ITALIC, Size);
                break;
            case 2: CurrentFont = new Font("Plain", Font.PLAIN, Size);
                break;
            default:
                CurrentFont = null;
        }
        return CurrentFont;
    }
    
    int CurrentFont = 0;
    int CurrentSize = 10;
    Color Black = new Color(0,0,0);
    Color Blue = new Color(0,0,255);
    Color Red = new Color(255,0,0);
    
    public JTextEditorV1() {
        initComponents();
        initComponent();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame1 = new javax.swing.JInternalFrame();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jRowLine = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        JText = new javax.swing.JEditorPane();
        CBFonts = new javax.swing.JComboBox();
        CBColor = new javax.swing.JComboBox();
        CBSize = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jName = new javax.swing.JTextField();
        SaveButton = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jInternalFrame1.setVisible(true);

        jRowLine.setColumns(20);
        jRowLine.setRows(5);
        jScrollPane2.setViewportView(jRowLine);

        jPanel1.add(jScrollPane2);

        jScrollPane3.setViewportView(JText);

        jPanel1.add(jScrollPane3);

        CBFonts.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBFonts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBFontsActionPerformed(evt);
            }
        });
        jPanel1.add(CBFonts);

        CBColor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBColorActionPerformed(evt);
            }
        });
        jPanel1.add(CBColor);

        CBSize.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        CBSize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBSizeActionPerformed(evt);
            }
        });
        jPanel1.add(CBSize);

        jInternalFrame1.getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jLabel1.setText("Nombre del Documento");
        jPanel2.add(jLabel1);
        jLabel1.getAccessibleContext().setAccessibleName("JName");

        jName.setColumns(20);
        jPanel2.add(jName);

        SaveButton.setText("Save");
        SaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveButtonActionPerformed(evt);
            }
        });
        jPanel2.add(SaveButton);

        jInternalFrame1.getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);

        add(jInternalFrame1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Exit the Application
     */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

    private void CBFontsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBFontsActionPerformed
        // TODO add your handling code here:
        JText.setFont(ObtainFont(CBFonts.getSelectedIndex(),CurrentSize));
        jRowLine.setFont(ObtainFont(CBFonts.getSelectedIndex(),CurrentSize));
        CurrentFont = CBFonts.getSelectedIndex();
    }//GEN-LAST:event_CBFontsActionPerformed

    private void CBColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBColorActionPerformed
        // TODO add your handling code here:
        switch(CBColor.getSelectedIndex()){
            case 0: JText.setForeground(Black);
                break;
            case 1: JText.setForeground(Blue);
                break;
            case 2: JText.setForeground(Red);                 
        }
    }//GEN-LAST:event_CBColorActionPerformed

    private void CBSizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBSizeActionPerformed
        // TODO add your handling code here:
        switch(CBSize.getSelectedIndex()){
            case 0: 
                JText.setFont(ObtainFont(CurrentFont,10));
                jRowLine.setFont(ObtainFont(CurrentFont,10));
                CurrentSize = 10;
                break;
            case 1: 
                JText.setFont(ObtainFont(CurrentFont,15));
                jRowLine.setFont(ObtainFont(CurrentFont,15));
                CurrentSize = 15;
                break;
            case 2: 
                JText.setFont(ObtainFont(CurrentFont,20));
                jRowLine.setFont(ObtainFont(CurrentFont,20));
                CurrentSize = 20;
        } 
    }//GEN-LAST:event_CBSizeActionPerformed

    private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
        // TODO add your handling code here:
        DocTitle article = new DocTitle();
        
    }//GEN-LAST:event_SaveButtonActionPerformed
    
    public void initComponent(){
        jRowLine.setColumns(3);
        jRowLine.setEditable(false);
        JText.getDocument().addDocumentListener(new DocumentListener(){
            public String getText(){
                int carPosition = JText.getDocument().getLength();
                javax.swing.text.Element root = JText.getDocument().getDefaultRootElement();
                String text = "1" + System.getProperty("line.separator");
                for(int i=2; i< root.getElementIndex(carPosition)+2;i++){
                    text+= i + System.getProperty("line.separator");
                }
                return text;
            }
            @Override()
            public void changedUpdate(DocumentEvent de){
                jRowLine.setText(getText());
            }
            @Override
            public void insertUpdate(DocumentEvent de) {
                jRowLine.setText(getText());
            }
 
            @Override
            public void removeUpdate(DocumentEvent de) {
                jRowLine.setText(getText());
            }
        });
        CBFonts.removeAllItems();
        CBColor.removeAllItems();
        CBSize.removeAllItems();
        CBFonts.addItem("Bold");
        CBFonts.addItem("Italic");
        CBFonts.addItem("Plain");
        CBColor.addItem("Black");
        CBColor.addItem("Blue");
        CBColor.addItem("Red");
        CBSize.addItem("10");
        CBSize.addItem("15");
        CBSize.addItem("20");
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JTextEditorV1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox CBColor;
    private javax.swing.JComboBox CBFonts;
    private javax.swing.JComboBox CBSize;
    private javax.swing.JEditorPane JText;
    private javax.swing.JButton SaveButton;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextArea jRowLine;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
